﻿package cn.huas.dao;
/**
 * 信计团队
 * @version 1.0
 * 网址：http://www.huas.cn
 * E-mail：xieqingcheng@126.com ; txws1010@163.com
 * Copyright 2011 Hunan University of Arts and Science
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.huas.model.Admin;
import cn.huas.model.Classes;
import cn.huas.model.Classroom;
import cn.huas.model.Course;
import cn.huas.model.CourseRClass;
import cn.huas.model.CourseRClassroom;
import cn.huas.model.Professional;
import cn.huas.model.Schedule;
import cn.huas.model.Systems;
import cn.huas.model.Teachers;
import cn.huas.model.TeachersRCourse;
import cn.huas.model.Teachingbuilding;
import cn.huas.modelVo.ArgsFlag;
import cn.huas.modelVo.JAll;
import cn.huas.modelVo.ScheduleByClass;
import cn.huas.util.ActionContextUtil;
import cn.huas.util.DBUtil;
@SuppressWarnings("unchecked")
public class RecordDAO {
	private Integer teacherId1 =0 ;
	private Integer classroomId1=0 ;
	private Integer courseId1=0;
	private Integer classesId1=0;
	private String jNumber1 = "";
	public HibernateTemplate hibernateTemplate;
	Connection conn =null;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
/*****************************************************Classroom模块************************************************/
	/**
	 * 添加教室  
	 * @author 
	 * @return Boolean
	 */
	/**
	 * @param classroom
	 * @return
	 * @throws Exception
	 */
	public Boolean addClassroom(Classroom classroom) throws Exception {
		boolean flag = true;
		try{
			if(classroom.getId()==null){
				classroom.setSysFlag("1");
				classroom.setCode(1000000);	
				classroom.setPaike_count(0);
			}else{
				List<Classroom> list = hibernateTemplate.find("select t from cn.huas.model.Classroom t ");
				classroom.setCode(list.get(list.size()-1).getCode()+1);
			}
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.clear();
			session.saveOrUpdate(classroom);
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}

	/**
	 * 查询教室
	 * @param args
	 * @return List<Classroom>
	 * @throws Exception
	 */
	public List<Classroom> queryClassroom(ArgsFlag args) throws Exception {
		try{
			String hql = "select t from cn.huas.model.Classroom t ";
			if(args!=null){
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}else{
					hql += " where t.id="+args.getFlag00();
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and t.id!="+args.getFlag01();
				}
				if(args.getFlag02()==null||"".equals(args.getFlag02())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachingbuilding.teachingbuilding_id="+args.getFlag02();
				}
				if(args.getFlag03()==null||"".equals(args.getFlag03())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachingbuilding.teachingbuilding_id!="+args.getFlag03();
				}
				if(args.getFlag10()==null||"".equals(args.getFlag10())){
					hql += " and 1=1";
				}else{
					hql += " and t.name='"+args.getFlag10()+"'";
				}
				if(args.getFlag11()==null||"".equals(args.getFlag11())){
					hql += " and 1=1";
				}else{
					hql += " and t.type='"+args.getFlag11()+"'";
				}
			}
			List<Classroom> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 删除教室
	 * @param ids
	 * @return Boolean
	 * @throws Exception
	 */
	public Boolean deleteClassroom(String ids) throws Exception {
		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int cid = Integer.parseInt(id);
					Classroom classroom = (Classroom)hibernateTemplate.get(Classroom.class, cid);
					hibernateTemplate.delete(classroom);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	
/**************************************班级模块***********************************************************/	
	
	/**
	 * 添加班级
	 * @author 
	 * @param Classes
	 * @return Boolean
	 */
	public Boolean addClasses(Classes classes) throws Exception {
		boolean flag = true;
		try{
			if(classes.getClasses_id()==null){
				classes.setClasses_sysFlag("1");
				classes.setClasses_code(100000);	
				classes.setClasses_password(classes.getClasses_name());
			}else{
				List<Classes> list = hibernateTemplate.find("select t from cn.huas.model.Classes t ");
				classes.setClasses_code((list.get(list.size()-1).getClasses_code()+1));
			}
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.clear();
			session.saveOrUpdate(classes);
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	 /**
	 * 查询班级
	 * @param args（班级id，）
	 * @return List<Classes>
	 * @throws Exception
	 */
	public List<Classes> queryClasses(ArgsFlag args) throws Exception {
		try{
			String hql = "select t from cn.huas.model.Classes t ";
			if(args!=null){
				//flag00条件班级id等于
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}else{
					hql += " where t.classes_id="+args.getFlag00();
				}
				//flag01判断班级id不等于
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and t.classes_id!="+args.getFlag01();
				}
				//flag02判断所属院系id等于
				if(args.getFlag02()==null||"".equals(args.getFlag02())){
					hql += " and 1=1";
				}else{
					hql += " and t.classes_systems.systems_id="+args.getFlag02();
				}
				//flag03判断所属专业id等于
				if(args.getFlag03()==null||"".equals(args.getFlag03())){
					hql += " and 1=1";
				}else{
					hql += " and t.classes_professional.professional_id="+args.getFlag03();
				}
				//flag10判断班级名称等于
				if(args.getFlag10()==null||"".equals(args.getFlag10())){
					hql += " and 1=1";
				}else{
					hql += " and t.classes_name='"+args.getFlag10()+"'";
				}
				//flag11判断院系名称等于
				if(args.getFlag11()==null||"".equals(args.getFlag11())){
					hql += " and 1=1";
				}else{
					hql += " and t.systems.systems_name='"+args.getFlag11()+"'";
				}
				//flag12判断院系名称等于
				if(args.getFlag12()==null||"".equals(args.getFlag12())){
					hql += " and 1=1";
				}else{
					hql += " and t.professional.professional_name='"+args.getFlag12()+"'";
				}
				//flag13判断班级年级等于
				if(args.getFlag13()==null||"".equals(args.getFlag13())){
					hql += " and 1=1";
				}else{
					hql += " and t.classes_grade='"+args.getFlag13()+"'";
				}
				
			}
			List<Classes> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 删除班级
	 * @param ids
	 * @return Boolean flag
	 * @throws Exception
	 */
	public Boolean deleteClasses(String ids) throws Exception {
		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int cid = Integer.parseInt(id);
					Classes Classes = (Classes)hibernateTemplate.get(Classes.class, cid);
					String hql = "select t from cn.huas.model.CourseRClass t where t.classes.classes_id= "+cid;
					List<CourseRClass> list = hibernateTemplate.find(hql);
					for(int i=0;i<list.size();i++){
						CourseRClass CourseRClass =(CourseRClass)list.get(i);
						hibernateTemplate.delete(CourseRClass);
					};
					hibernateTemplate.delete(Classes);
				}
			}

		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	
/*********************************************教师模块**************************************************************/	
	
	/**
	 * 添加教师
	 * @return boolean
	 * @param Teachers
	 */
	public boolean addTeachers(Teachers teachers) {
		boolean flag = true;  
		try{
			if(teachers.getTeachers_id()==null){
				teachers.setTeachers_sysFlag("1");
				teachers.setTeachers_password(teachers.getTeachers_name());
				teachers.setTeachers_code(10000);
			}else{
				List<Teachers> list = hibernateTemplate.find("select t from cn.huas.model.Teachers t ");
				teachers.setTeachers_code(list.get(list.size()-1).getTeachers_code()+1);
			}
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.clear();
			session.saveOrUpdate(teachers);
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	/**
	 * 查询教师
	 * @param ArgsFlag args
	 * @return List<Teachers>
	 */
	public List<Teachers> queryTeachers(ArgsFlag args) throws Exception {

		try{
			String hql = "select t from cn.huas.model.Teachers t ";
			if(args!=null){
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}else{
					hql += " where t.teachers_id="+args.getFlag00();
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachers_id!="+args.getFlag01();
				}
				if(args.getFlag02()==null||"".equals(args.getFlag02())){
					hql += " and 1=1";
				}else{
					hql += " and t.systems.systems_id="+args.getFlag02();
				}
				if(args.getFlag03()==null||"".equals(args.getFlag03())){
					hql += " and 1=1";
				}else{
					hql += " and t.systems.systems_id!="+args.getFlag03();
				}
				if(args.getFlag10()==null||"".equals(args.getFlag10())){
					hql += " and 1=1";
				}else{   
					hql += " and t.teachers_name='"+args.getFlag10()+"'";
				}
				if(args.getFlag11()==null||"".equals(args.getFlag11())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachers_systems.systems_name='"+args.getFlag11()+"'";
				}
				if(args.getFlag12()==null||"".equals(args.getFlag12())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachers_zhichen='"+args.getFlag12()+"'";
				}
			}
			System.out.println(hql);
			List<Teachers> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 删除教师
	 * @param  String ids
	 * @return Boolean flag 
	 */
	public Boolean deleteTeachers(String ids) {

		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int teachers_id = Integer.parseInt(id);
					String hql1 = "select t from cn.huas.model.TeachersRCourse t where t.teachers.teachers_id= "+teachers_id;
					List<TeachersRCourse> list1 = hibernateTemplate.find(hql1);
					for(int i=0;i<list1.size();i++){
						TeachersRCourse TeachersRCourse1 =(TeachersRCourse)list1.get(i);
						hibernateTemplate.delete(TeachersRCourse1);
						
					};
					Teachers Teachers=(Teachers)hibernateTemplate.get(Teachers.class, teachers_id);
					hibernateTemplate.delete(Teachers);
				}
			}
  
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	
	}
/**********************************************课程模块*******************************************************/
	
	public boolean delFromTeachersRCourse(String teachersIds, String courseIds) {
		boolean flag = true;
		try {
		String[] courses={}; 
		String[] teachers={}; 
		if(courseIds.length() != 0 && courseIds != null && courseIds != "") {
			courseIds = courseIds.substring(0, courseIds.length() - 1);
			courses = courseIds.split(",");
			if(courses[courses.length-1]==""){
				String []newcourses= new String[courses.length-1];
				for(int j=0;j<courses.length-1;j++){
					newcourses[j]=courses[j];
				}
				courses=newcourses;
			}else{}
		}else{}
		
		if(teachersIds.length() != 0 && teachersIds != null && teachersIds != "") {
			teachersIds = teachersIds.substring(0, teachersIds.length() - 1);
			teachers = teachersIds.split(",");
			if(teachers[teachers.length-1]==""){
				String [] newteachers= new String[teachers.length-1];
				for(int j=0;j<courses.length-1;j++){
					newteachers[j]=teachers[j];
				}
				teachers=newteachers;
			}
			else{}
		}else{}
		
		for (String course_id : courses) 
			for (String teacher_id : teachers) {
				ArgsFlag args= new ArgsFlag();
				args.setFlag02(Integer.valueOf(course_id));
				args.setFlag04(Integer.valueOf(teacher_id));
				List<TeachersRCourse> list =queryTeachersRCourse(args);
				if(list!=null &&list.size()>0){
					hibernateTemplate.delete(list.get(0));
				}else{
					continue;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
		}
	
	
	/**
	 * 查询课程
	 * @author 
	 * @param args
	 * @return List<Cousre>
	 */
	public List<TeachersRCourse> queryTeachersRCourse(ArgsFlag args) { 
		try{   
			String hql = "select t from cn.huas.model.TeachersRCourse t ";
			if(args!=null){
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}else{
					hql += " where t.id="+args.getFlag00();
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and t.id!="+args.getFlag01();
				}
				if(args.getFlag02()==null||"".equals(args.getFlag02())){
					hql += " and 1=1";
				}else{
					hql += " and t.course.course_id="+args.getFlag02();
				}
				if(args.getFlag03()==null||"".equals(args.getFlag03())){
					hql += " and 1=1";
				}else{
					hql += " and t.course.course_id!="+args.getFlag03();
				}
				if(args.getFlag04()==null||"".equals(args.getFlag04())){
					hql += " and 1=1";
				}else{ 
					hql += " and t.teachers.teachers_id='"+args.getFlag04()+"'";
				}
				
				if(args.getFlag10()==null||"".equals(args.getFlag10())){
					hql += " and 1=1";
				}else{
					hql += " and t.course.course_name='"+args.getFlag10()+"'";
				}
				if(args.getFlag11()==null||"".equals(args.getFlag11())){
					hql += " and 1=1";
				}else{ 
					hql += " and t.teachers.teachers_name='"+args.getFlag11()+"'";
				}
				if(args.getFlag12()==null||"".equals(args.getFlag12())){
					hql += " and 1=1";
				}else{
					hql += " and t.course.course_type='"+args.getFlag12()+"'";
				}
			}
			System.out.println(hql);
			List<TeachersRCourse> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}


	
	/**********************************************课程模块*******************************************************/
	/**
	 * 新增课程
	 * @param Course
	 * @return boolean
	 * @author 
	 */
	
	public boolean addCourse(Course course) {
		boolean flag = true;  
		try{
			if(course.getCourse_id()==null){
				course.setCourse_sysFlag("1");
				course.setCourse_code(10000000);
			}
			else{
				List<Course> list = hibernateTemplate.find("select t from cn.huas.model.Course t ");
				course.setCourse_code(list.get(list.size()-1).getCourse_code()+1);
			}
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.clear();
			session.saveOrUpdate(course);
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	
	/**
	 * 查询课程
	 * @author 
	 * @param args
	 * @return List<Cousre>
	 */
	public List<Course> queryCourse(ArgsFlag args) { 
		try{   
			String hql = "select t from cn.huas.model.Course t ";
			if(args!=null){
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}else{
					hql += " where t.id="+args.getFlag00();
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and t.id!="+args.getFlag01();
				}
				if(args.getFlag02()==null||"".equals(args.getFlag02())){
					hql += " and 1=1";
				}else{
					hql += " and t.course_id="+args.getFlag02();
				}
				if(args.getFlag03()==null||"".equals(args.getFlag03())){
					hql += " and 1=1";
				}else{
					hql += " and t.course_id!="+args.getFlag03();
				}
				
				if(args.getFlag10()==null||"".equals(args.getFlag10())){
					hql += " and 1=1";
				}else{
					hql += " and t.course_name='"+args.getFlag10()+"'";
				}
				if(args.getFlag12()==null||"".equals(args.getFlag12())){
					hql += " and 1=1";
				}else{
					hql += " and t.course_type='"+args.getFlag12()+"'";
				}
			}
			System.out.println(hql);
			List<Course> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}


	/**
	 * 删除课程
	 * @author 
	 * @param String ids
	 * @return boolean
	 */
	public Boolean deleteCourse(String ids) {

		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int course_id = Integer.parseInt(id);
					String hql = "select t from cn.huas.model.CourseRClass t where t.course.course_id= "+course_id;
					List<CourseRClass> list = hibernateTemplate.find(hql);
					for(int i=0;i<list.size();i++){
						CourseRClass CourseRClass =(CourseRClass)list.get(i);
						hibernateTemplate.delete(CourseRClass);
						
					};
					String hql1 = "select t from cn.huas.model.TeachersRCourse t where t.course.course_id= "+course_id;
					List<TeachersRCourse> list1 = hibernateTemplate.find(hql1);
					for(int i=0;i<list1.size();i++){
						TeachersRCourse TeachersRCourse1 =(TeachersRCourse)list1.get(i);
						hibernateTemplate.delete(TeachersRCourse1);
						
					};
					String hql2 = "select t from cn.huas.model.CourseRClassroom t where t.course.course_id= "+course_id;
					List<CourseRClassroom> list2 = hibernateTemplate.find(hql2);
					for(int i=0;i<list2.size();i++){
						CourseRClassroom CourseRClassroom =(CourseRClassroom)list2.get(i);
						hibernateTemplate.delete(CourseRClassroom);
						
					};
					Course course=(Course)hibernateTemplate.get(Course.class, course_id);
					hibernateTemplate.delete(course);
				}
			}
  
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	
	}

	
	
/**********************************************系院模块*****************************************************/
	/**
	 * 新增系院
	 * @author 
	 * @param Systems systems
	 * @return boolean
	 */
	public Boolean addSystems(Systems systems) throws Exception {
		boolean flag = true;
		try{
			if(systems.getSystems_id()==null){
				systems.setSystems_sysFlag("1");
				systems.setSystems_code(10000);	
			}else{
				List<Systems> list = hibernateTemplate.find("select t from cn.huas.model.Systems t ");
				systems.setSystems_code(list.get(list.size()-1).getSystems_code()+1);	
			}
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.clear();
			session.saveOrUpdate(systems);
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	/***
	 * systems 
	 * name : "flag1", fieldLabel : "院系名称", 
	 * name : "flag0", fieldLabel :"院系编号",
	 */
	public List<Systems> querySystems(ArgsFlag args) throws Exception {
		try{
			String hql = "select t from cn.huas.model.Systems t ";
			if(args!=null){
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}else{
					hql += " where t.systems_id="+args.getFlag00();
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and t.systems_id!="+args.getFlag01();
				}
				if(args.getFlag10()==null||"".equals(args.getFlag10())){
					
					hql += " and 1=1";
				}else{
					hql += " and t.systems_name='"+args.getFlag10()+"'";
				}
			}
			System.out.println(hql);
			List<Systems> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 删除院系
	 * @param ids
	 * @return boolean
	 * @throws Exception
	 */
	public Boolean deleteSystems(String ids) throws Exception {
		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int cid = Integer.parseInt(id);
					Systems Systems = (Systems)hibernateTemplate.get(Systems.class, cid);
					hibernateTemplate.delete(Systems);
				}
			}

		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}

	
/*******************************************教学楼*************************************************/
	
	/** 
	 * 新增教学楼
	 * @author 
	 * @return boolean
	 * @param Teachingbuilding
	 */
	public boolean addTeachingbuilding(Teachingbuilding teachingbuilding) {
		boolean flag = true;
		try{
			if(teachingbuilding.getTeachingbuilding_id()==null){
				teachingbuilding.setTeachingbuilding_sysFlag("1");
				teachingbuilding.setTeachingbuilding_code(1000);	
			}else{
				List<Teachingbuilding> list = hibernateTemplate.find("select t from cn.huas.model.Teachingbuilding t ");
				teachingbuilding.setTeachingbuilding_code(list.get(list.size()-1).getTeachingbuilding_code()+1);
			}
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.clear();
			session.saveOrUpdate(teachingbuilding);
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	/**
	 * 查询教学楼
	 * Teachingbuilding 
	 * @name : "flag1", fieldLabel : "教学楼名称", 
	 * @name : "flag0",fieldLabel : "教学楼编号",
	 * @name : "flag2", fieldLabel : "所属院系",
	 * @param ArgsFlag args
	 * @return List<Teachingbuilding>
	 */
	public List<Teachingbuilding> queryTeachingbuilding(ArgsFlag args) {
		try{
			String hql = "select t from cn.huas.model.Teachingbuilding t ";
			if(args!=null){
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}else{
					hql += " where t.teachingbuilding_id="+args.getFlag00();
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachingbuilding_id!="+args.getFlag01();
				}
				if(args.getFlag02()==null||"".equals(args.getFlag02())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachingbuilding_systems.systems_id="+args.getFlag02();
				}
				if(args.getFlag10()==null||"".equals(args.getFlag10())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachingbuilding_name='"+args.getFlag10()+"'";
				}
				if(args.getFlag11()==null||"".equals(args.getFlag11())){
					hql += " and 1=1";
				}else{
					hql += " and t.teachingbuilding_systems.systems_name='"+args.getFlag11()+"'";
				}
				
			}
			List<Teachingbuilding> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 删除教学楼
	 * @param ids
	 * @author 
	 * @return boolean
	 */
	public boolean deleteTeachingbuilding(String ids) {
		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int cid = Integer.parseInt(id);
					Teachingbuilding Teachingbuilding = (Teachingbuilding)hibernateTemplate.get(Teachingbuilding.class, cid);
					hibernateTemplate.delete(Teachingbuilding);
				}
			}

		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}


/*********************************************管理员模块***********************************************/
	
	/**
	 * 新增管理员
	 * @author 
	 * @return boolean
	 * @param Admin
	 */
	public Boolean addAdmin(Admin Admin) throws Exception {
		boolean flag = true;
		try{
			if(Admin.getAdmin_id()==null){
				Admin.setAdmin_sysFlag("1");
					Admin.setAdmin_code(10);	
			}else{
				List<Admin> list = hibernateTemplate.find("select t from cn.huas.model.Admin t ");	
				Admin.setAdmin_code(list.get(list.size()-1).getAdmin_code()+1);
			}
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			session.clear();
			session.saveOrUpdate(Admin);
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	/** 
	 * 查询Admin
	 * 根据 id name ",
	 * @return List<Admin>
	 * @param ArgsFlag args
	 */
	public List<Admin> queryAdmin(ArgsFlag args) {
		try{
			String hql = "select t from cn.huas.model.Admin t ";
			if(args!=null){
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}
				else{ 
					hql += " where t.admin_id='"+args.getFlag00();
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}
				else{ 
					hql += " and t.admin_id !="+args.getFlag01();
				}
				if(args.getFlag10()==null||"".equals(args.getFlag10())){//flag0:用户姓名（admin_name）
					hql += " and 1=1";
				}
				else{ 
					hql += " and t.admin_name='"+args.getFlag10()+"'";
				}
				
			}
			List<Admin> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 删除管理员
	 * @author 
	 * @param ids
	 * @return
	 */
	public boolean deleteAdmin(String ids) {
		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int cid = Integer.parseInt(id);
					Admin Admin = (Admin)hibernateTemplate.get(Admin.class, cid);
					hibernateTemplate.delete(Admin);
				}
			}

		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}


/*******************************************专业模块*******************************************************/
	
	/**
	 * 新增专业
	 * @author 
	 * @param Professional
	 * @return boolean
	 */
	public Boolean addProfessional(Professional professional) throws Exception {
		boolean flag = true;
		try{
			if(professional.getProfessional_id()==null){
				professional.setProfessional_sysFlag("1");
				professional.setProfessional_code(100);	
			}else{
				List<Professional> list = hibernateTemplate.find("select t from cn.huas.model.Professional t ");
				professional.setProfessional_code(list.get(list.size()-1).getProfessional_code()+1);
			}
			hibernateTemplate.saveOrUpdate(professional);
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	
	
	/**
	 * 查询专业
	 * name : "flag0", fieldLabel : "所属院系", name : "flag2", fieldLabel : "专业名称",
	 * name : "flag1", fieldLabel : "专业编号",
	 * @author 
	 * @param ArgsFlag args
	 * @return List<professional>
	 */
	public List<Professional> queryProfessional(ArgsFlag args) throws Exception {
		try{
			String hql = "select t from cn.huas.model.Professional t ";
			if(args!=null){
				if(args.getFlag00()==null||"".equals(args.getFlag00())){
					hql += " where 1=1";
				}else{
					hql += " where t.professional_id="+args.getFlag00();
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and t.professional_id!="+args.getFlag01();
				}
				
				if(args.getFlag02()==null||"".equals(args.getFlag02())){
					hql += " and 1=1";
				}else{
					hql += " and t.professional_systems.systems_id="+args.getFlag02();
				}
				
				if(args.getFlag10()==null||"".equals(args.getFlag10())){
					hql += " and 1=1";
				}else{
					hql += " and t.professional_name='"+args.getFlag10()+"'";
				}
				if(args.getFlag11()==null||"".equals(args.getFlag11())){
					hql += " and 1=1";
				}else{
					hql += " and t.professional_systems.systems_name='"+args.getFlag11()+"'";
				}
			}
			System.out.println(hql);
			List<Professional> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}

	/**
	 * 删除专业
	 * @param ids
	 * @return boolaen
	 * @author 
	 * @throws Exception
	 */
	public Boolean deleteProfessional(String ids) throws Exception {
		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int cid = Integer.parseInt(id);
					Professional Professional = (Professional)hibernateTemplate.get(Professional.class, cid);
					hibernateTemplate.delete(Professional);
				}
			}

		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	

	/********************************************用户登录模块**************************************************/
	//-------------管理员登陆-----------
	 public boolean loginAdmin(Admin admin) throws Exception{
		 HttpServletRequest request=ActionContextUtil.getHttpServletRequest();
		 boolean flag =false;
		List<Admin>	lists = hibernateTemplate
			.find("from cn.huas.model.Admin admin where admin.admin_name = '"

					+ admin.getAdmin_name().trim() + "' and admin.admin_password = '" + admin.getAdmin_password().trim() + "'");

		

		 if(lists.size()>0){
			 flag = true;
		 }else{
			 flag = false;
			 request.setAttribute("message1", "用户名或密码错误！");
		 }
		 return flag;
	 }
   //教师登陆
	public boolean loginTeachers(Teachers teachers) {
		 HttpServletRequest request=ActionContextUtil.getHttpServletRequest();
		 boolean flag =false;
		List<Teachers>	lists = hibernateTemplate
			.find("from cn.huas.model.Teachers teachers where teachers.teachers_name = '"
					+ teachers.getTeachers_name().trim() + "' and teachers.teachers_password = '" + teachers.getTeachers_password().trim() + "'");
		
		 if(lists.size()>0){
			 flag = true;
		 }else{
			 flag = false;
			 request.setAttribute("message1", "用户名或密码错误！");
		 }
		 return flag;
	}
  //班级登陆
	public boolean loginClasses(Classes classes) {
		 HttpServletRequest request=ActionContextUtil.getHttpServletRequest();
		 boolean flag =false;
		List<Classes>	lists = hibernateTemplate
			.find("from cn.huas.model.Classes classes where classes.name = '"
					+ classes.getClasses_name().trim() + "' and classes.password = '" + classes.getClasses_password().trim() + "'");
		 if(lists.size()>0){
			 flag = true;
		 }else{
			 flag = false;
			 request.setAttribute("message1", "用户名或密码错误！");
		 }
		 return flag;
	}
	
/********************************************排课模块**************************************************/
	/******************************************** 排课模块 **************************************************/
	public boolean paike() throws Exception {
		Connection conn = null;
		PreparedStatement ps11 = null;
		PreparedStatement ps21 = null;
		PreparedStatement ps31 = null;

		PreparedStatement ps12 = null;
		PreparedStatement ps22 = null;
		PreparedStatement ps32 = null;
		PreparedStatement pshb = null;

		ResultSet rs11 = null;
		ResultSet rs21 = null;
		ResultSet rs31 = null;
		List<Schedule> lists = hibernateTemplate
				.find("from cn.huas.model.Schedule ");
		for (int i = 0; i < lists.size(); i++) {

			System.out
					.println("select from t_schedule_plan where t_schedule_id='"
							+ lists.get(i).getSchedule_id() + "'");
			// 得到第各条排课记录
			List list11 = new ArrayList();
			List list21 = new ArrayList();
			List list31 = new ArrayList();

			String sql11 = "select j01,j02,j03,j04,j05,j06,j07,j08,j09,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20 from t_class_schedule where c_class_id = '"
					+ lists.get(i).getClass_id() + "'";
			String sql21 = "select j01,j02,j03,j04,j05,j06,j07,j08,j09,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20 from t_teacher_schedule where c_teacher_id = '"
					+ lists.get(i).getTeacher_id() + "'";
			String sql31 = "select j01,j02,j03,j04,j05,j06,j07,j08,j09,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20 from t_classroom_schedule where c_classroom_id = '"
					+ lists.get(i).getClassroom_id() + "'";

			System.out.println(sql11);
			System.out.println(sql21);
			System.out.println(sql31);

			conn = DBUtil.createConn();
			ps11 = DBUtil.prepare(conn, sql11);
			ps21 = DBUtil.prepare(conn, sql21);
			ps31 = DBUtil.prepare(conn, sql31);
			try {
				rs11 = ps11.executeQuery();
				rs21 = ps21.executeQuery();
				rs31 = ps31.executeQuery();

				while (rs11.next()) {
					for (int k = 0; k < 20; k++) {
						if (k < 9) {
							list11.add(k, rs11.getInt("j0" + (k + 1) + ""));
						} else if (k >= 9) {
							list11.add(k, rs11.getInt("j" + (k + 1) + ""));
						}
					}

				}
				while (rs21.next()) {
					for (int m = 0; m < 20; m++) {
						if (m < 9) {
							list21.add(m, rs21.getInt("j0" + (m + 1) + ""));
						} else if (m >= 9) {
							list21.add(m, rs21.getInt("j" + (m + 1) + ""));
						}
					}
				}
				while (rs31.next()) {
					for (int n = 0; n < 20; n++) {
						if (n < 9) {
							list31.add(n, rs31.getInt("j0" + (n + 1) + ""));
						} else if (n >= 9) {
							list31.add(n, rs31.getInt("j" + (n + 1) + ""));
						}
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				DBUtil.close(rs11);
				DBUtil.close(rs21);
				DBUtil.close(rs31);
				DBUtil.close(ps11);
				DBUtil.close(ps21);
				DBUtil.close(ps31);

			}

			// 求三个集合的并集,放到一个新的集合mylist中
			List mylist = new ArrayList();
			for (int p = 0; p < 20; p++) {
				mylist.add(p, (Integer) list11.get(p) + (Integer) list21.get(p)
						+ (Integer) list31.get(p));
			}

			// 遍历mylist，找出空闲位置，也就是值为零的位置,并把所有的位置放到一个新的集合
			List kxlist = new ArrayList();// kxlist中放置空闲位置
			List zhylist = new ArrayList();// zhylist放置已经占用的位置
			for (int j = 0; j < mylist.size(); j++) {
				int n = 0;
				n = Integer.parseInt(mylist.get(j).toString());
				if (n == 0) {
					kxlist.add(j);
				} else {
					zhylist.add(j);
				}
			}

			for (int kk = 0; kk < kxlist.size(); kk++) {
				System.out.println("重叠后可用的位置：" + kxlist.get(kk));
			}

			// 根据课时数，在空闲位置中随机取出相应个数的位置数，并保存位置值到hashset中（避免了重复位置）
			// 排除课时为2、3时连续一天内上两次
			ArrayList<Integer> xxlist = new ArrayList<Integer>();// xxlist放置随机产生的位置
			int weektimes = (lists.get(i).getWeek_times())/2;
			int xhcount=0;//陷入死循环时记录死循环的次数
			try {
				while (true) {
					System.out.println("问题处1");
					Random rm = new Random();
					int u = (int) (rm.nextDouble() * (kxlist.size()));
					if (xxlist.size() <= 0) {
						xxlist.add(u);
						if (xxlist.size() != weektimes) {
							continue;
						}
					}
					System.out.println("问题处2");
					// 时间段相隔差大于4表示不在同一天
					if ((!xxlist.contains(u))
							&& xxlist.size() == 1
							&& Math.abs((Integer) kxlist.get(u)
									- (Integer) kxlist.get((Integer) (xxlist
											.get(0)))) > 3) {
						xxlist.add(u);
						if (xxlist.size() != weektimes) {
							continue;
						}
					}
					if ((!xxlist.contains(u))
							&& (xxlist.size() == 2)
							&& Math.abs((Integer) kxlist.get(u)
									- (Integer) kxlist.get(xxlist.get(0))) > 3
							&& Math.abs((Integer) kxlist.get(u)
									- (Integer) kxlist.get(xxlist.get(1))) > 3) {

						xxlist.add(u);
					}
					if (xxlist.size() >= weektimes) {
						break;
					}

					System.out.println("问题处3");
					xhcount++;
					if(xhcount==10000){
						break;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(xhcount==10000){
				String sqlhb = "update t_schedule_plan set c_hbclass_id='1' where c_schedule_id='"+lists.get(i).getSchedule_id()+"'";
				System.out.println(sqlhb);
				try {
					pshb = conn.prepareStatement(sqlhb);
					pshb.executeUpdate();
					System.out.println("出现甩课的排课号是："+lists.get(i).getSchedule_id());
					System.out.println("出现甩课！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
					}catch(SQLException  e){
						e.printStackTrace();
					DBUtil.close(pshb);
					DBUtil.close(conn);
					}
				continue;
			}
			
			
           //通过随机产生的位置得到其对应的真正位置，并更新其中的值为排课号
			for (int xx = 0; xx < xxlist.size(); xx++) {
				list11.set((Integer) kxlist.get(xxlist.get(xx)), lists.get(i)
						.getSchedule_id());
				list21.set((Integer) kxlist.get(xxlist.get(xx)), lists.get(i)
						.getSchedule_id());
				list31.set((Integer) kxlist.get(xxlist.get(xx)), lists.get(i)
						.getSchedule_id());

				System.out.println("取出来的位置是："
						+ (Integer) kxlist.get(xxlist.get(xx)));
			}

			// 把list1、2、3更新的数据更新到数据库中
			String sql12 = "update t_class_schedule set";
			String sql22 = "update t_teacher_schedule set";
			String sql32 = "update t_classroom_schedule set";
			for (int q = 0; q < 20; q++) {
				if (q < 9) {
					sql12 += " j0" + (q + 1) + "='" + list11.get(q) + "', ";
					sql22 += " j0" + (q + 1) + "='" + list21.get(q) + "', ";
					sql32 += " j0" + (q + 1) + "='" + list31.get(q) + "', ";
				} else if (q >= 9 && q < 19) {
					sql12 += " j" + (q + 1) + "='" + list11.get(q) + "', ";
					sql22 += " j" + (q + 1) + "='" + list21.get(q) + "', ";
					sql32 += " j" + (q + 1) + "='" + list31.get(q) + "', ";
				} else if (q == 19) {
					sql12 += " j" + (q + 1) + "='" + list11.get(q) + "' ";
					sql22 += " j" + (q + 1) + "='" + list21.get(q) + "' ";
					sql32 += " j" + (q + 1) + "='" + list31.get(q) + "' ";
				}
			}
			sql12 += " where c_class_id = '" + lists.get(i).getClass_id()
					+ "' ";
			sql22 += " where c_teacher_id = '" + lists.get(i).getTeacher_id()
					+ "' ";
			sql32 += " where c_classroom_id = '"
					+ lists.get(i).getClassroom_id() + "' ";

			System.out.println(sql12);
			System.out.println(sql22);
			System.out.println(sql32);

			try {
				ps12 = conn.prepareStatement(sql12);
				ps22 = conn.prepareStatement(sql22);
				ps32 = conn.prepareStatement(sql32);
				ps12.executeUpdate();
				ps22.executeUpdate();
				ps32.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
				DBUtil.close(ps12);
				DBUtil.close(ps22);
				DBUtil.close(ps32);
			}
			DBUtil.close(conn);
		}

		return true;

	}
   
   /**
    *@author xie
    *@return boolean
    *@param Map map 
    * 查询排课时执行插入的课程的那节课的时间段信息，如教室、老师是否已经排了课
    */
   public String queryPaiKe(Map map){
	   String flag = "";
	   String flag1 = "";
	   PreparedStatement ps11 = null;
	   PreparedStatement ps21 = null;
	   PreparedStatement ps31 = null;
	   
	   PreparedStatement ps12 = null;
	   PreparedStatement ps22 = null;
	   PreparedStatement ps32 = null;
	   PreparedStatement ps42 = null;
	   
	   ResultSet rs11 = null;
	   ResultSet rs21 = null;
	   ResultSet rs31 = null;
	   try{
			Set<Map.Entry<String, Object>> set = map.entrySet();
	        for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it.hasNext();) {
	            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
	            if("scheduleTitle".equals(entry.getKey())){
	            	String scheduleTitle = (String)entry.getValue();
	            }else{
	            	String jnumber = entry.getKey().trim();
	            	int scheduleId = (Integer)entry.getValue();
	            	jNumber1 = jnumber;//放到deletePaike()中找到t_class_schedule、t_classroom_schedule、t_teacher_scheudle;
	            	Schedule schedule = (Schedule) hibernateTemplate.get(Schedule.class,scheduleId);
	     		    String sql11 = "select "+jnumber+" from t_class_schedule where c_class_id = '"+schedule.getClass_id()+"'";
	     		    String sql21 = "select "+jnumber+" from t_teacher_schedule where c_teacher_id = '"+schedule.getTeacher_id()+"'";
	     		    String sql31 = "select "+jnumber+" from t_classroom_schedule where c_classroom_id = '"+schedule.getClassroom_id()+"'";
	     		    
	     		    System.out.println("----------------------------------查询插入一节课时有无冲突，已用上！---------------------------");
	    		    System.out.println(sql11);
	    		    System.out.println(sql21);
	    		    System.out.println(sql31);
	    		    
	     		    conn = DBUtil.createConn();
	    		    ps11 = DBUtil.prepare(conn, sql11);
	    		    ps21 = DBUtil.prepare(conn, sql21);
	    		    ps31 = DBUtil.prepare(conn, sql31);
	    		    
	    		    rs11 = ps11.executeQuery();
	    		    String jj1 = ""; 
	    		    while(rs11.next()){
	    		    	jj1=rs11.getString(jnumber);
	    		    	if(!"0".equals(jj1)){
		    		    	JAll jAll = setJAll(jj1);
		    		    	classesId1 = jAll.getClassId();//放到deletePaike()中在t_class_scheulde的classId
		    		    	System.out.println("班级"+jAll.getClassName()+"于"+jnumber+"在"+jAll.getClassroomName()+"上"+jAll.getCourseName()+"("+jAll.getTeacherName()+")！");
		    		    	flag1 += "班级"+jAll.getClassName()+"于本节课在"+jAll.getClassroomName()+"上"+jAll.getCourseName()+"("+jAll.getTeacherName()+")！\n ";
		    		    	continue;
		    		    }
	    		    }
	    		    rs21 = ps21.executeQuery();
	    		    String jj2 = ""; 
	    		    while(rs21.next()){
	    		    	jj2=rs21.getString(jnumber);
	    		    	if(!"0".equals(jj2)){
		    		    	JAll jAll = setJAll(jj2);
		    		    	teacherId1 = jAll.getTeacherId();//放到deletePaike()中在t_teacher_scheulde的teacherId
		    		    	System.out.println("老师"+jAll.getTeacherName()+"于"+jnumber+"已安排给"+jAll.getClassName()+"上"+jAll.getCourseName()+"课("+jAll.getClassroomName()+")！ ");
		    		    	flag1 += "老师"+jAll.getTeacherName()+"于本节课已安排给"+jAll.getClassName()+"上"+jAll.getCourseName()+"课("+jAll.getClassroomName()+")！\n ";
		    		    }
	    		    }
	    		    
	    		    rs31 = ps31.executeQuery();
	    		    String jj3 = "";
	    		    while(rs31.next()){
	    		    	jj3 =rs31.getString(jnumber);
	    		    	if(!"0".equals(jj3)){
		    		    	JAll jAll = setJAll(jj3);
		    		    	classroomId1 = jAll.getClassroomId();//放到deletePaike()中在t_classroom_scheulde的classroomId
		    		    	System.out.println("教室"+jAll.getClassroomName()+"于"+jnumber+"已安排给"+jAll.getClassName()+"上"+jAll.getCourseName()+"("+jAll.getTeacherName()+")课！");
		    		    	flag1 += "教室"+jAll.getClassroomName()+"于本节课已安排给"+jAll.getClassName()+"上"+jAll.getCourseName()+"("+jAll.getTeacherName()+")课！\n";
		    		    }
	    		    }
	    		 }
	        }
	        System.out.println(flag1);
	        if(!"".equals(flag1)){
	        	flag = flag1;
	        }else{
	        	System.out.println("----------------------------------此次查询出没有冲突！------------------------------------");
	        }
	        return flag;
	   }catch(Exception e){
	        e.printStackTrace();
	        return flag;
	   }
 }
   
   /**
    *@author xie
    *@return boolean
    *@param Map map 
    * 更新课表
    */
   public boolean addPaiKe(Map map){
	   boolean flag = false;
	   PreparedStatement ps11 = null;
	   PreparedStatement ps21 = null;
	   PreparedStatement ps31 = null;
	   
	   PreparedStatement ps12 = null;
	   PreparedStatement ps22 = null;
	   PreparedStatement ps32 = null;
	   
	   PreparedStatement ps13 = null;
	   PreparedStatement ps23 = null;
	   PreparedStatement ps33 = null;
	   
	   try{
		   if(jNumber1!=""){
			   System.out.println("-------------------------有冲突且确认更新，添加之前先删除以前排课占了位置的课程--------------------------------");
			   if(teacherId1!=null||teacherId1!=0){
				   String sql01 = "update t_teacher_schedule set "+jNumber1+"=0 where c_teacher_id = '"+teacherId1+"'";
				   System.out.println("------------------------------根据teacherId更新t_teacher_scheudle------------------------------");
				   System.out.println(sql01);
				   ps13 = conn.prepareStatement(sql01);
				   ps13.executeUpdate();
			   }
			   if(classroomId1!=null||classroomId1!=0){
				   String sql02 = "update t_classroom_schedule set "+jNumber1+"=0 where c_classroom_id = '"+classroomId1+"'";
				   System.out.println("------------------------------根据classroomId更新t_classroom_scheudle------------------------------");
				   System.out.println(sql02);
				   ps23 = conn.prepareStatement(sql02);
				   ps23.executeUpdate();
			   }
			   if(classesId1!=null||classesId1!=0){
				   String sql03 = "update t_class_schedule set "+jNumber1+"=0 where c_class_id = '"+classesId1+"'";
				   System.out.println("------------------------------根据classId更新t_class_scheudle------------------------------");
				   System.out.println(sql03);
				   ps33 = conn.prepareStatement(sql03);
				   ps33.executeUpdate();
			   }
			   System.out.println("-------------------------删除之前排课占了位置的课程,执行完毕！--------------------------------");
		   }
			Set<Map.Entry<String, Object>> set = map.entrySet();
	        for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it.hasNext();) {
	            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
	            if("scheduleTitle".equals(entry.getKey())){
	            	String scheduleTitle = (String)entry.getValue();
	            }else if("classId".equals(entry.getKey())){
	            	String classId = (String)entry.getValue();
	            }else{
	            	String jnumber = entry.getKey().trim();
	            	int scheduleId = (Integer)entry.getValue();
	            	System.out.println("排课计划的ID是:"+scheduleId);
	            	Schedule schedule = (Schedule) hibernateTemplate.get(Schedule.class,scheduleId);
	             
	    		    String sql12 = "update t_class_schedule set "+jnumber+"= "+scheduleId+ " where c_class_id = '"+schedule.getClass_id()+"'";
	     		    String sql22 = "update t_teacher_schedule set "+jnumber+"= "+scheduleId+ " where c_teacher_id = '"+schedule.getTeacher_id()+"'";
	     		    String sql32 = "update t_classroom_schedule set "+jnumber+"= "+scheduleId+" where c_classroom_id = '"+schedule.getClassroom_id()+"'";
	    		    
	     		    System.out.println("------------------------------在课表中插入一节课  RecordDao类addPaiKe()---------------------------------");
	    		    System.out.println(sql12);
	    		    System.out.println(sql22);
	    		    System.out.println(sql32);
	    		    
	     		    ps12 = conn.prepareStatement(sql12);
	    		    ps22 = conn.prepareStatement(sql22);
	    		    ps32 = conn.prepareStatement(sql32);
	    		    
	    		    ps12.executeUpdate();
	    		    ps22.executeUpdate();
	    		    ps32.executeUpdate();
	    		    System.out.println("------------------------------在课表中插入节课执行完毕  RecordDao类addPaiKe()-------------------------------------------");
	            }
	        }
		}catch (Exception e ){
			e.printStackTrace();
			flag = false;
			DBUtil.close(ps11);
	    	DBUtil.close(ps21);
	    	DBUtil.close(ps31);
			DBUtil.close(ps12);
	    	DBUtil.close(ps22);
	    	DBUtil.close(ps32);
	    	DBUtil.close(ps13);
	    	DBUtil.close(ps23);
	    	DBUtil.close(ps33);
		}
		DBUtil.close(conn);
		return flag;
	   }
   
   /**
    *@author xie
    *@return boolean
    *@param Map map 
    * 初始化课表
    */
   public boolean deletePaiKe(Map map){
	   boolean flag = false;
	   PreparedStatement ps13 = null;
	   PreparedStatement ps23 = null;
	   PreparedStatement ps33 = null;
	   ResultSet rs13 = null;
	   ResultSet rs23 = null;
	   ResultSet rs33 = null;
	   try{
			Set<Map.Entry<String, Object>> set = map.entrySet();
	        for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it.hasNext();) {
	            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
	            if("scheduleTitle".equals(entry.getKey())){
	            	String scheduleTitle = (String)entry.getValue();
	            }else{
	            	String jnumber = entry.getKey().trim();
	            	int scheduleId = (Integer)entry.getValue();
	            	Schedule schedule = (Schedule) hibernateTemplate.get(Schedule.class,scheduleId);
	     		    
	     		    System.out.println("----------------------------------删除课表中一节课 ReCordDao类deletePaike()---------------------------");
	     		    String sql13 = "update t_class_schedule set "+jnumber+"='0'  where c_class_id = '"+schedule.getClass_id()+"'";
	     		    String sql23 = "update t_teacher_schedule set "+jnumber+"='0'  where c_teacher_id = '"+schedule.getTeacher_id()+"'";
	     		    String sql33 = "update t_classroom_schedule set "+jnumber+"='0'  where c_classroom_id = '"+schedule.getClassroom_id()+"'";
	     		    //String sql13 = "update t_class_schedule set j01='0',j02='0',j03='0',j04='0',j05='0',j06='0',j07='0',j08='0',j09='0',j10='0',j11='0',j12='0',j13='0',j14='0',j15='0',j16='0',j17='0',j18='0',j19='0',j20='0'  where c_class_id = '"+schedule.getClass_id()+"'";
	     		   
	     		    System.out.println(sql13);
	     		    System.out.println(sql23);
	     		    System.out.println(sql33);
	     		    conn = DBUtil.createConn();
	     		    ps13 = conn.prepareStatement(sql13);
	     		    ps23 = conn.prepareStatement(sql23);
	     		    ps33 = conn.prepareStatement(sql33);
	     		    ps13.executeUpdate();
	     		    ps23.executeUpdate();
	     		    ps33.executeUpdate();
	     		   System.out.println("------------------------------删除执行完毕ReCordDao类deletePaike()-------------------------------------------");
	            }
	        }
		}catch (Exception e ){
			e.printStackTrace();
			flag = false;
			DBUtil.close(ps13);
			DBUtil.close(ps23);
			DBUtil.close(ps33);
		}
		DBUtil.close(conn);
		return flag;
	   }

   /** 
	 * 查询班级与课程关联表
	 * @author Xie 
	 * @return List
	 */


	public List<CourseRClass> queryCouseRClass(ArgsFlag args){
		try{
			String hql = "select r from cn.huas.model.CourseRClass r";
			if(args!=null||"".equals(args)){
				if(args.getFlag00()!=null||"".equals(args.getFlag00())){//flag2:课程Id
					hql += " where r.classes.id = "+args.getFlag00();
				}
			}
			List<CourseRClass> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/*
	 * 根据班级Id查询教学计划
	 */
   public List<Schedule> queryScheduleByClassId(ArgsFlag args){
	   try{
			String hql = "select r from cn.huas.model.Schedule r";
			if(args!=null||"".equals(args)){
				if(args.getFlag00()!=null||"".equals(args.getFlag00())){//flag2:课程Id
					hql += " where r.class_id = "+args.getFlag00();
				}
			}
			System.out.println("---------通过课程Id查询出t_schedule_class  RecordDao类queryScheduleByClassId()--------------");
			System.out.println(hql);
			List<Schedule> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
   }
	
   /*
	* 根据教室Id查询教学计划
	*/
   public List<Schedule> queryScheduleByClassroomId(ArgsFlag args){
	   try{
			String hql = "select r from cn.huas.model.Schedule r";
			if(args!=null||"".equals(args)){
				if(args.getFlag00()!=null||"".equals(args.getFlag00())){
					hql += " where r.classroom_id = "+args.getFlag00();
				}
			}
			List<Schedule> list = hibernateTemplate.find(hql);
			System.out.println("---------------根据教室Id查询教学计划 RecordDao类queryScheduleByClassroomId------------------");
			System.out.println(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
   }
   
   /*
	* 根据老师Id查询教学计划
	*/
   public List<Schedule> queryScheduleByTeacherId(ArgsFlag args){
	   try{
			String hql = "select r from cn.huas.model.Schedule r";
			if(args!=null||"".equals(args)){
				if(args.getFlag00()!=null||"".equals(args.getFlag00())){
					hql += " where r.teacher_id = "+args.getFlag00();
				}
			}
			List<Schedule> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
   }
   
   /*
	* 根据课程Id查询教学计划
	*/
  public List<Schedule> queryScheduleByCourseId(String courseId){
	   try{
			String hql = "select r from cn.huas.model.Schedule r";
			if(courseId!=null||"".equals(courseId)){
					hql += " where r.course_id = '"+courseId +"'";
			}
			List<Schedule> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
  }
  
  /*
	* 根据课程Id查询教学计划
	*/
public List<Schedule> queryScheduleByScheduleId(String courseId){
	   try{
			String hql = "select r from cn.huas.model.Schedule r";
			if(courseId!=null||"".equals(courseId)){
					hql += " where r.schedule_id = '"+courseId +"'";
			}
			List<Schedule> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
}
   
   /*
	 * 根据多个条件查询教学计划
	 */
  public List<Schedule> querySchedule(ArgsFlag args){
	   try{
			String hql = "select r from cn.huas.model.Schedule r";
			if(args!=null||"".equals(args)){
				if(args.getFlag00()!=null||"".equals(args.getFlag00())){//教室Id
					hql += " where r.classroom_id = "+args.getFlag00();
				}else{
					hql += " where 1=1";
				}
				if(args.getFlag01()==null||"".equals(args.getFlag01())){
					hql += " and 1=1";
				}else{
					hql += " and r.class_id ="+args.getFlag01();//班级id
				}
				if(args.getFlag02()==null||"".equals(args.getFlag02())){
					hql += " and 1=1";
				}else{
					hql += " and r.teacher_id="+args.getFlag02();//老师id
				}
				if(args.getFlag03()==null||"".equals(args.getFlag03())){
					hql += " and 1=1";
				}else{
					hql += " and r.course_id="+args.getFlag03();//课程id
				}
				if(args.getFlag04()==null||"".equals(args.getFlag04())){
					hql += " and 1=1";
				}else{
					hql += " and r.schedule_id="+args.getFlag04();//课程id
				}
			}
			List<Schedule> list = hibernateTemplate.find(hql);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
  }
  
  /*
	 * 根据班级Id查询班级排课
	 */
 public List<ScheduleByClass> queryPaikeByClassId(ArgsFlag args){
	 List list = new ArrayList();  
	 try{
		   String sql = "select j01,j02,j03,j04,j05,j06,j07,j08,j09,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20 from t_class_schedule where c_class_id = '";
			if(args!=null||"".equals(args)){
				if(args.getFlag01()!=null||"".equals(args.getFlag01())){//flag2:课程Id
					sql += args.getFlag01()+"'";
				}
			}
			conn = DBUtil.createConn();
			PreparedStatement ps = DBUtil.prepare(conn, sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				ScheduleByClass scheduleByClass = new ScheduleByClass();
				JAll jAll01 = this.setJAll( rs.getString("j01"));
				JAll jAll02 = this.setJAll( rs.getString("j02"));
				JAll jAll03 = this.setJAll( rs.getString("j03"));
				JAll jAll04 = this.setJAll( rs.getString("j04"));
				JAll jAll05 = this.setJAll( rs.getString("j05"));
				JAll jAll06 = this.setJAll( rs.getString("j06"));
				JAll jAll07 = this.setJAll( rs.getString("j07"));
				JAll jAll08 = this.setJAll( rs.getString("j08"));
				JAll jAll09 = this.setJAll( rs.getString("j09"));
				JAll jAll10 = this.setJAll( rs.getString("j10"));
				JAll jAll11 = this.setJAll( rs.getString("j11"));
				JAll jAll12 = this.setJAll( rs.getString("j12"));
				JAll jAll13 = this.setJAll( rs.getString("j13"));
				JAll jAll14 = this.setJAll( rs.getString("j14"));
				JAll jAll15 = this.setJAll( rs.getString("j15"));
				JAll jAll16 = this.setJAll( rs.getString("j16"));
				JAll jAll17 = this.setJAll( rs.getString("j17"));
				JAll jAll18 = this.setJAll( rs.getString("j18"));
				JAll jAll19 = this.setJAll( rs.getString("j19"));
				JAll jAll20 = this.setJAll( rs.getString("j20"));
				scheduleByClass.setClassId(args.getFlag01());
				scheduleByClass.setJ01(jAll01);
				scheduleByClass.setJ02(jAll02);
				scheduleByClass.setJ03(jAll03);
				scheduleByClass.setJ04(jAll04);
				scheduleByClass.setJ05(jAll05);
				scheduleByClass.setJ06(jAll06);
				scheduleByClass.setJ07(jAll07);
				scheduleByClass.setJ08(jAll08);
				scheduleByClass.setJ09(jAll09);
				scheduleByClass.setJ10(jAll10);
				scheduleByClass.setJ11(jAll11);
				scheduleByClass.setJ12(jAll12);
				scheduleByClass.setJ13(jAll13);
				scheduleByClass.setJ14(jAll14);
				scheduleByClass.setJ15(jAll15);
				scheduleByClass.setJ16(jAll16);
				scheduleByClass.setJ17(jAll17);
				scheduleByClass.setJ18(jAll18);
				scheduleByClass.setJ19(jAll19);
				scheduleByClass.setJ20(jAll20);
				list.add(scheduleByClass);
			}
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
 }
/**
 * 将前台需要的数据放到JALL中，用于输出
 * @author xie
 * @return JAll
 * @param  ArgsFlag args ,String id
 * 
 */
 public JAll setJAll(String id)throws Exception{
		JAll jAll = new JAll();
		if("0".equals(id)){
			return jAll;
		}else{
			//jAll.setClassId(args.getFlag01());
			jAll.setScheduleId(Integer.parseInt(id));
			
			//通过schedule查询教学计划，在教学计划中课程唯一
			//Schedule schedule = queryScheduleByScheduleId(String.valueOf(jAll.getScheduleId())).get(0);
			Schedule schedule = (Schedule)hibernateTemplate.get(Schedule.class, Integer.valueOf(id));
			
			//通过教学计划查询课程
			//ArgsFlag argsCourse = new ArgsFlag();
			//argsCourse.setFlag00(schedule.getCourse_id());
			//Course course = this.queryCourse(argsCourse).get(0);
			Course course = (Course)hibernateTemplate.get(Course.class, schedule.getCourse_id());
			
			//通过教学计划查询教室
			//ArgsFlag argsClassroom = new ArgsFlag();
			//argsClassroom.setFlag00(schedule.getCourse_id());
			//Classroom classroom = this.queryClassroom(argsClassroom).get(0);
			Classroom classroom = (Classroom)hibernateTemplate.get(Classroom.class, schedule.getClassroom_id());
			
			
			//通过教学计划查询班级
			//ArgsFlag argsClasses = new ArgsFlag();
			//argsClasses.setFlag00(schedule.getClass_id());
			//Classes classes = this.queryClasses(argsClasses).get(0);
			Classes classes = (Classes)hibernateTemplate.get(Classes.class, schedule.getClass_id());
			
			//通过教学计划查询老师
			//ArgsFlag argsTeacher = new ArgsFlag();
			//argsTeacher.setFlag00(schedule.getTeacher_id());
			//Teachers teacher = this.queryTeachers(argsTeacher).get(0);
			Teachers teacher = (Teachers)hibernateTemplate.get(Teachers.class, schedule.getTeacher_id());
			
			Double d = Double.valueOf(course.getCourse_totaltime())/Double.valueOf(String.valueOf(schedule.getWeek_times()));
			Double dd = Math.ceil(d);
			DecimalFormat df=new DecimalFormat("#");
			System.out.println(df.format(dd));
			
			jAll.setClassName(classes.getClasses_name());
			jAll.setClassId(classes.getClasses_id());
			jAll.setClassroomName(classroom.getTeachingbuilding().getTeachingbuilding_name()+"-"+classroom.getName());
			jAll.setClassroomId(classroom.getId());
			jAll.setCourseId(course.getCourse_id());
			jAll.setCourseName(course.getCourse_name());
			jAll.setTotalTime(course.getCourse_totaltime());
			jAll.setWeekTime(String.valueOf(schedule.getWeek_times()));
			jAll.setTeacherId(teacher.getTeachers_id());
			jAll.setTeacherName(teacher.getTeachers_name());
			jAll.setWeeks(df.format(dd));
			
			
			return jAll;
		}
 }
 /*******************************课程模块***********************************************************/


	/**
	 * 增加教师与课程的关联
	 * @param teachers 
	 * 
	 * @param args
	 * @return List
	 */
	public boolean addToTeachersRCourse(String course_ids,String ids) {
		boolean flag = true;
		try {
				Session session = getHibernateTemplate()
				.getSessionFactory().getCurrentSession();
					session.clear();
				String[] courses={}; 
				String[] teachers={}; 
				if(course_ids.length() != 0 && course_ids != null && course_ids != "") {
					course_ids = course_ids.substring(0, course_ids.length() - 1);
					courses = course_ids.split(",");
					if(courses[courses.length-1]==""){
						String []newcourses= new String[courses.length-1];
						for(int j=0;j<courses.length-1;j++){
							newcourses[j]=courses[j];
						}
						courses=newcourses;
					}else{}
				}else{}
				if(ids.length() != 0 && ids != null && ids != "") {
					ids = ids.substring(0, ids.length() - 1);
					teachers = ids.split(",");
					if(teachers[teachers.length-1]==""){
						String []newteachers= new String[teachers.length-1];
						for(int j=0;j<courses.length-1;j++){
							newteachers[j]=teachers[j];
						}
						courses=newteachers;
					}
					else{}
				}else{}
				for (String course_id : courses) 
					for (String teachers_id : teachers) {
						ArgsFlag args= new ArgsFlag();
						args.setFlag02(Integer.valueOf(course_id));
						args.setFlag04(Integer.valueOf(teachers_id));
						List<TeachersRCourse> list =queryTeachersRCourse(args);
						if(list!=null &&list.size()>0){
							continue;
						}else{
							TeachersRCourse teachersRCourse= new TeachersRCourse();
							Course course=new Course();
							course.setCourse_id(Integer.valueOf(course_id));
							teachersRCourse.setCourse(course);
							Teachers teacher= new Teachers();
							teacher.setTeachers_id(Integer.valueOf(teachers_id));
							teachersRCourse.setTeachers(teacher);
							teachersRCourse.setSysFlag("1");
							session.saveOrUpdate(teachersRCourse);
					}
					
				}
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}

	public boolean delFromCourseRClass(String classesIds, String courseIds) {
		boolean flag = true;
		try {
		String[] courses={}; 
		String[] classes={}; 
		if(courseIds.length() != 0 && courseIds != null && courseIds != "") {
			courseIds = courseIds.substring(0, courseIds.length() - 1);
			courses = courseIds.split(",");
			if(courses[courses.length-1]==""){
				String []newcourses= new String[courses.length-1];
				for(int j=0;j<courses.length-1;j++){
					newcourses[j]=courses[j];
				}
				courses=newcourses;
			}else{}
		}else{}
		
		if(classesIds.length() != 0 && classesIds != null && classesIds != "") {
			classesIds = classesIds.substring(0, classesIds.length() - 1);
			classes = classesIds.split(",");
			if(classes[classes.length-1]==""){
				String [] newclasses= new String[classes.length-1];
				for(int j=0;j<courses.length-1;j++){
					newclasses[j]=classes[j];
				}
				classes=newclasses;
			}
			else{}
		}else{}
		
		for (String course_id : courses) 
			for (String classes_id : classes) {
				ArgsFlag args= new ArgsFlag();
				args.setFlag00(Integer.valueOf(course_id));
				args.setFlag03(Integer.valueOf(classes_id));
				List<CourseRClass> list =queryCourseRClass(args);
				if(list!=null &&list.size()>0){
					hibernateTemplate.delete(list.get(0));
				}else{
					continue;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
		}
	/**
	 * 增加班级与课程的关联
	 * 
	 * @param args
	 * @return List
	 */
	public boolean addToCourseRClass(String course_ids,String ids) {
		boolean flag = true;
		try {
		Session session = getHibernateTemplate()
		.getSessionFactory().getCurrentSession();
			session.clear();
		String[] courses={}; 
		String[] classess={}; 
		if(course_ids.length() != 0 && course_ids != null && course_ids != "") {
			course_ids = course_ids.substring(0, course_ids.length() - 1);
			courses = course_ids.split(",");
			if(courses[courses.length-1]==""){
				String []newcourses= new String[courses.length-1];
				for(int j=0;j<courses.length-1;j++){
					newcourses[j]=courses[j];
				}
				courses=newcourses;
			}else{}
		}else{}
		if(ids.length() != 0 && ids != null && ids != "") {
			ids = ids.substring(0, ids.length() - 1);
			classess = ids.split(",");
			if(classess[classess.length-1]==""){
				String [] newclasses= new String[classess.length-1];
				for(int j=0;j<courses.length-1;j++){
					newclasses[j]=classess[j];
				}
				courses=newclasses;
			}
			else{}
		}else{}
		for (String course_id : courses) 
			for (String classes_id : classess) {	
				ArgsFlag args= new ArgsFlag();
				args.setFlag00(Integer.valueOf(course_id));
				args.setFlag03(Integer.valueOf(classes_id));
				List<CourseRClass> list =queryCourseRClass(args);
				if(list!=null &&list.size()>0){
					continue;
				}else{
					CourseRClass classesRCourse= new CourseRClass();
					Course course=new Course();
					course.setCourse_id(Integer.valueOf(course_id));
					classesRCourse.setCourse(course);
					Classes classes= new Classes();
					classes.setClasses_id(Integer.valueOf(classes_id));
					classesRCourse.setClasses(classes);
					classesRCourse.setSysFlag("1");
					session.saveOrUpdate(classesRCourse);
					
				}
			}
} catch (Exception e) {
	e.printStackTrace();
	flag = false;
}
return flag;}

	/**
	 * 查询班级与课程的关联
	 * 
	 * @param args
	 * @return List
	 */
	public List<CourseRClass> queryCourseRClass(ArgsFlag argsFlag) {
		try {
			String hql = "select r from cn.huas.model.CourseRClass r";
			if (argsFlag != null || "".equals(argsFlag)) {
				if (argsFlag.getFlag00() == null
						|| "".equals(argsFlag.getFlag00())) {// flag2:课程id
					hql += " where 1=1 " ;
				}else{
					hql += " where r.course.course_id = " + argsFlag.getFlag00();
				}
				if (argsFlag.getFlag03() == null
						|| "".equals(argsFlag.getFlag03())) {// flag2:Teachser_id
					hql += " and 1=1 ";
				}else{
					hql += " and r.classes.classes_id = " + argsFlag.getFlag03();
					
				}
			}
			List<CourseRClass> list = hibernateTemplate.find(hql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 自动生成计划表
	 * 
	 * 
	 */
	public boolean scheduleJihua() throws Exception{
		String hql1 = "select c from  cn.huas.model.Classes c";
		List<Classes> list1 = hibernateTemplate.find(hql1);
		boolean flag = false;
		int count = 0;
		//遍历所有的班级
		for(int i = 0 ; i<list1.size(); i++){
			int classroomid = 0;
			int courseid = 0;
			int teacherid = 0;
			int weektimes = 0;
			String hql2 = "select cc from cn.huas.model.CourseRClass cc where cc.classes.classes_id = " + list1.get(i).getClasses_id();
			List<CourseRClass> list2 = hibernateTemplate.find(hql2);
			int classid = list1.get(i).getClasses_id();
			//遍历所取班级的对应课程
			for(int j = 0;j<list2.size();j++){
				String hql3 = "select tc from cn.huas.model.TeachersRCourse tc where tc.course.course_id = " +list2.get(j).getCourse().getCourse_id();
				List<TeachersRCourse> list3 = hibernateTemplate.find(hql3);
				courseid = list2.get(j).getCourse().getCourse_id();
				weektimes =Integer.parseInt(list2.get(j).getCourse().getCourse_week());
				//遍历所取课程所对应的教师
				for(int k = 0 ;k<list3.size();k++){
					String hql4 = "select t from cn.huas.model.Teachers t where t.teachers_id = " + list3.get(k).getTeachers().getTeachers_id();
					List<Teachers> list4 = hibernateTemplate.find(hql4);
					int m = list4.get(0).getPaike_count();
					System.out.println("教师："+list4.get(0).getTeachers_id()+"被取出来的次数："+m);
					//判断该教师被选取的次数小于5
					if(m<4){
						teacherid = list4.get(0).getTeachers_id();
						list4.get(0).setPaike_count(m+1);
						hibernateTemplate.saveOrUpdate(list4.get(0));//更新该实体所对应的数据库
						break;
					}else if(m<4&&k==list3.size()-1){
						System.out.println("课程："+list2.get(j).getCourse()+"课程排课选不到相应教室，请重新关联教师！");
						break;
					}
					else{
						continue;
					}
				}
				//遍历可以选取的教室
				if(list2.get(j).getCourse().getCourse_classroom_type().trim().equals("多媒体教室")){
					String hql51 = "select cr from cn.huas.model.Classroom cr where cr.type='多媒体教室'";
					List<Classroom> list51 = hibernateTemplate.find(hql51);
					for(int qq = 0;qq<list51.size();qq++){
						int u = list51.get(qq).getPaike_count();
						//判断一个教室被选取的次数小于4
						if(u<5){
							//选取多媒体教室
							classroomid = list51.get(qq).getId();
							list51.get(qq).setPaike_count(u+1);
							hibernateTemplate.saveOrUpdate(list51.get(qq));
							break;
						}else{
							continue;
						}
					}
				}else{
					String hql5 = "select cr from cn.huas.model.Classroom cr where cr.type='普通教室'";
					List<Classroom> list5 = hibernateTemplate.find(hql5);
					for(int q = 0;q<list5.size();q++){
						int u = list5.get(q).getPaike_count();
						//判断一个教室被选取的次数小于4
						if(u<4){
							classroomid = list5.get(q).getId();
							list5.get(q).setPaike_count(u+1);
							hibernateTemplate.saveOrUpdate(list5.get(q));
							break;
						}else{
							continue;
						}
					}
				}
				
				//更新数据到数据库计划表中
				String sql = "insert into t_schedule_plan(c_class_id,c_classroom_id,c_teachers_id,c_course_id,c_week_times,c_hbclass_id) values("+classid+","+classroomid+","+teacherid+","+courseid+","+weektimes+",0) ";
				System.out.println(sql);
				conn = DBUtil.createConn();
				PreparedStatement ps = null;
			  	ps = DBUtil.prepare(conn, sql);
			  	count += ps.executeUpdate();
			  	DBUtil.close(ps);
			}
			
		}
		DBUtil.close(conn);
		if(count>0){
			flag = true;
		}
		return flag;
	}

	  /**
	 * 删除教学计划
	 * @param ids
	 * @author 
	 * @return boolean
	 */
	public boolean deleteSchedule(String ids) {
		Boolean flag = true;
		try{
			if(ids.length()!=0 && ids!=null && ids!=""){
				ids = ids.substring(0,ids.length()-1);
				String[] str = ids.split(",");
				for(String id : str){
					int cid = Integer.parseInt(id);
					System.out.println("删除教学计划的Id为："+cid);
					Schedule schedule = (Schedule)hibernateTemplate.get(Schedule.class, cid);
					hibernateTemplate.delete(schedule);
				}
			}

		}catch(Exception e){
			System.out.println("删除失败！");
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
	/**@author xie
	  * date 2012-05-10
	  * 根据教室Id查询班级排课
	  */
	public List<ScheduleByClass> queryPaikeByClassroomId(ArgsFlag args){
	List list = new ArrayList();  
	try{
		   String sql = "select j01,j02,j03,j04,j05,j06,j07,j08,j09,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20 from t_classroom_schedule where c_classroom_id = '";
			if(args!=null||"".equals(args)){
				if(args.getFlag01()!=null||"".equals(args.getFlag01())){//flag2:教室Id
					sql += args.getFlag01()+"'";
				}
			}
			conn = DBUtil.createConn();
			PreparedStatement ps = DBUtil.prepare(conn, sql);
			System.out.println("-------------根据教室Id查询教室的占有情况----------------");
			System.out.println(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				ScheduleByClass scheduleByClass = new ScheduleByClass();
				JAll jAll01 = this.setJAll( rs.getString("j01"));
				JAll jAll02 = this.setJAll( rs.getString("j02"));
				JAll jAll03 = this.setJAll( rs.getString("j03"));
				JAll jAll04 = this.setJAll( rs.getString("j04"));
				JAll jAll05 = this.setJAll( rs.getString("j05"));
				JAll jAll06 = this.setJAll( rs.getString("j06"));
				JAll jAll07 = this.setJAll( rs.getString("j07"));
				JAll jAll08 = this.setJAll( rs.getString("j08"));
				JAll jAll09 = this.setJAll( rs.getString("j09"));
				JAll jAll10 = this.setJAll( rs.getString("j10"));
				JAll jAll11 = this.setJAll( rs.getString("j11"));
				JAll jAll12 = this.setJAll( rs.getString("j12"));
				JAll jAll13 = this.setJAll( rs.getString("j13"));
				JAll jAll14 = this.setJAll( rs.getString("j14"));
				JAll jAll15 = this.setJAll( rs.getString("j15"));
				JAll jAll16 = this.setJAll( rs.getString("j16"));
				JAll jAll17 = this.setJAll( rs.getString("j17"));
				JAll jAll18 = this.setJAll( rs.getString("j18"));
				JAll jAll19 = this.setJAll( rs.getString("j19"));
				JAll jAll20 = this.setJAll( rs.getString("j20"));
				scheduleByClass.setClassroomId(args.getFlag01());
				scheduleByClass.setJ01(jAll01);
				scheduleByClass.setJ02(jAll02);
				scheduleByClass.setJ03(jAll03);
				scheduleByClass.setJ04(jAll04);
				scheduleByClass.setJ05(jAll05);
				scheduleByClass.setJ06(jAll06);
				scheduleByClass.setJ07(jAll07);
				scheduleByClass.setJ08(jAll08);
				scheduleByClass.setJ09(jAll09);
				scheduleByClass.setJ10(jAll10);
				scheduleByClass.setJ11(jAll11);
				scheduleByClass.setJ12(jAll12);
				scheduleByClass.setJ13(jAll13);
				scheduleByClass.setJ14(jAll14);
				scheduleByClass.setJ15(jAll15);
				scheduleByClass.setJ16(jAll16);
				scheduleByClass.setJ17(jAll17);
				scheduleByClass.setJ18(jAll18);
				scheduleByClass.setJ19(jAll19);
				scheduleByClass.setJ20(jAll20);
				list.add(scheduleByClass);
			}
			System.out.println("-------------根据教室Id查询教室的占有情况，查询完毕！我爱玲玲----------------");
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**@author xie
	 * date 2012-05-10
	 * 根据老师Id查询班级排课
	 */
	public List<ScheduleByClass> queryPaikeByTeacherId(ArgsFlag args){
	List list = new ArrayList();  
	try{
		   String sql = "select j01,j02,j03,j04,j05,j06,j07,j08,j09,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20 from t_teacher_schedule where c_teacher_id = '";
			if(args!=null||"".equals(args)){
				if(args.getFlag01()!=null||"".equals(args.getFlag01())){//flag2:老师Id
					sql += args.getFlag01()+"'";
				}
			}
			conn = DBUtil.createConn();
			PreparedStatement ps = DBUtil.prepare(conn, sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				ScheduleByClass scheduleByClass = new ScheduleByClass();
				JAll jAll01 = this.setJAll( rs.getString("j01"));
				JAll jAll02 = this.setJAll( rs.getString("j02"));
				JAll jAll03 = this.setJAll( rs.getString("j03"));
				JAll jAll04 = this.setJAll( rs.getString("j04"));
				JAll jAll05 = this.setJAll( rs.getString("j05"));
				JAll jAll06 = this.setJAll( rs.getString("j06"));
				JAll jAll07 = this.setJAll( rs.getString("j07"));
				JAll jAll08 = this.setJAll( rs.getString("j08"));
				JAll jAll09 = this.setJAll( rs.getString("j09"));
				JAll jAll10 = this.setJAll( rs.getString("j10"));
				JAll jAll11 = this.setJAll( rs.getString("j11"));
				JAll jAll12 = this.setJAll( rs.getString("j12"));
				JAll jAll13 = this.setJAll( rs.getString("j13"));
				JAll jAll14 = this.setJAll( rs.getString("j14"));
				JAll jAll15 = this.setJAll( rs.getString("j15"));
				JAll jAll16 = this.setJAll( rs.getString("j16"));
				JAll jAll17 = this.setJAll( rs.getString("j17"));
				JAll jAll18 = this.setJAll( rs.getString("j18"));
				JAll jAll19 = this.setJAll( rs.getString("j19"));
				JAll jAll20 = this.setJAll( rs.getString("j20"));
				scheduleByClass.setTeacherId(args.getFlag01());
				scheduleByClass.setJ01(jAll01);
				scheduleByClass.setJ02(jAll02);
				scheduleByClass.setJ03(jAll03);
				scheduleByClass.setJ04(jAll04);
				scheduleByClass.setJ05(jAll05);
				scheduleByClass.setJ06(jAll06);
				scheduleByClass.setJ07(jAll07);
				scheduleByClass.setJ08(jAll08);
				scheduleByClass.setJ09(jAll09);
				scheduleByClass.setJ10(jAll10);
				scheduleByClass.setJ11(jAll11);
				scheduleByClass.setJ12(jAll12);
				scheduleByClass.setJ13(jAll13);
				scheduleByClass.setJ14(jAll14);
				scheduleByClass.setJ15(jAll15);
				scheduleByClass.setJ16(jAll16);
				scheduleByClass.setJ17(jAll17);
				scheduleByClass.setJ18(jAll18);
				scheduleByClass.setJ19(jAll19);
				scheduleByClass.setJ20(jAll20);
				list.add(scheduleByClass);
			}
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 查询班级与课程的关联
	 * 
	 * @param args
	 * @return List
	 */
	public List<TeachersRCourse> queryTeacherRCourse(ArgsFlag argsFlag) {
		try {
			String hql = "select r from cn.huas.model.TeachersRCourse r";
			if (argsFlag != null || "".equals(argsFlag)) {
				if (argsFlag.getFlag00() == null
						|| "".equals(argsFlag.getFlag00())) {// flag00:课程id班级id
					hql += " where 1=1 " ;
				}else{
					hql += " where r.course.course_id = " + argsFlag.getFlag00();
				}
				if (argsFlag.getFlag03() == null
						|| "".equals(argsFlag.getFlag03())) {// flag03:老师id
					hql += " and 1=1 ";
				}else{
					hql += " and r.teachers.teachers_id = " + argsFlag.getFlag03();
					
				}
			}
			System.out.println("查询教师与课程的关联表：");
			System.out.println(hql);
			List<TeachersRCourse> list = hibernateTemplate.find(hql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 添加班级
	 * @author 
	 * @param Classes
	 * @return Boolean
	 */
	public Boolean addSchedule(ArgsFlag args) throws Exception {
		boolean flag = true;
		//PreparedStatement ps = null;
		try{
			if(args!=null||"".equals(args)){//教室Id//班级id//老师id//课程id
				Schedule schedule = new Schedule();
				schedule.setClassroom_id(args.getFlag00());
				schedule.setClass_id(args.getFlag01());
				schedule.setTeacher_id(args.getFlag02());
				schedule.setCourse_id(args.getFlag03());
				schedule.setWeek_times(args.getFlag04());
				ArgsFlag args1 = new ArgsFlag();
				args1.setFlag00(args.getFlag00());
				args1.setFlag01(args.getFlag01());
				args1.setFlag02(args.getFlag02());
				args1.setFlag03(args.getFlag03());
				List<Schedule> list = querySchedule(args1);
				if(list.size()==0){
					System.out.println("添加教学计划：");
					System.out.println("classroomId:"+args.getFlag00()+"\nteacherId:"+args.getFlag01()+"\nclassID:"+args.getFlag02()+"\ncourseId:"+args.getFlag03());
					hibernateTemplate.saveOrUpdate(schedule);
				}else{
					System.out.println("已有记录，不进行添加！");
					flag =false;
				}
			}else{
				flag = false;
			}
		}catch(Exception e){
			e.printStackTrace();
			flag =false;
		}
		return flag;
	}
	
}
